//
//  SearHomeTableViewCell.m
//  BGM
//
//  Created by 我要变好看 on 2019/1/2.
//  Copyright © 2019 com.alirtc.demo. All rights reserved.
//

#import "SearHomeTableViewCell.h"
#import "Common.h"

@interface SearHomeTableViewCell ()
@property (nonatomic, strong)UILabel *titleLabel;
@property (nonatomic, strong)UILabel *rightLabel;
@property (nonatomic, strong)UIView *bgView;

@property (nonatomic, strong)UIButton *btn1;
@property (nonatomic, strong)UIButton *btn2;
@property (nonatomic, strong)UIButton *btn3;
@property (nonatomic, strong)UISwitch *rightSwitch;


@end


@implementation SearHomeTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self != nil) {
        
        self.backgroundColor = bgmColor(244, 244, 244);
        
        self.bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, 55)];
        self.bgView.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.bgView];
        self.bgView.userInteractionEnabled = YES;
        
        self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 100, 60)];
        self.titleLabel.font = [UIFont systemFontOfSize:16];
        [self.bgView addSubview:self.titleLabel];
        
        self.rightLabel = [[UILabel alloc]initWithFrame:CGRectMake(80,0,SCREENW-110,60)];
        self.rightLabel.font = [UIFont systemFontOfSize:14];
        self.rightLabel.textAlignment = NSTextAlignmentRight;
        [self.bgView addSubview:self.rightLabel];
        
        self.btn1 = [[UIButton alloc]initWithFrame:CGRectMake(SCREENW-220, 15, 60, 30)];
        [self.btn1 setTitle:@"男生" forState:UIControlStateNormal];
        self.btn1.titleLabel.font = [UIFont systemFontOfSize:14];
        self.btn1.layer.cornerRadius = 10;
        self.btn1.layer.masksToBounds = YES;
        self.btn1.tag = 100;
        [self.btn1 setTitleColor:bgmgrayColor forState:UIControlStateNormal];
        [self.bgView addSubview:self.btn1];
        
        self.btn2 = [[UIButton alloc]initWithFrame:CGRectMake(SCREENW-150, 15, 60, 30)];
        [self.btn2 setTitle:@"女生" forState:UIControlStateNormal];
        self.btn2.titleLabel.font = [UIFont systemFontOfSize:14];
        self.btn2.layer.cornerRadius = 10;
        self.btn2.layer.masksToBounds = YES;
        [self.btn2 setTitleColor:bgmgrayColor forState:UIControlStateNormal];
        [self.bgView addSubview:self.btn2];
        
        self.btn3 = [[UIButton alloc]initWithFrame:CGRectMake(SCREENW-80, 15, 60, 30)];
        [self.btn3 setTitle:@"不限" forState:UIControlStateNormal];
        self.btn3.titleLabel.font = [UIFont systemFontOfSize:14];
        self.btn3.layer.cornerRadius = 10;
        self.btn3.layer.masksToBounds = YES;
        [self.btn3 setTitleColor:bgmgrayColor forState:UIControlStateNormal];
        [self.bgView addSubview:self.btn3];
        
        self.rightSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(SCREENW-70, 15, 60, 30)];
        self.rightSwitch.onTintColor = bgmyelloeColor;
        [self addSubview:self.rightSwitch];
        [self.rightSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];

        self.btn1.tag = 1;
        self.btn2.tag = 2;
        self.btn3.tag = 3;
        
        [self.btn1 addTarget:self action:@selector(clickSexBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self.btn2 addTarget:self action:@selector(clickSexBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self.btn3 addTarget:self action:@selector(clickSexBtn:) forControlEvents:UIControlEventTouchUpInside];
   
    }
    return self;
}

- (void)searchHomeTitle:(NSString *)title rightText:(NSString *)rightTxt btn:(BOOL)b swith:(BOOL)swith isPublic:(NSString *)isPublic seleStr:(NSString *)seleStr{
    
    self.titleLabel.text = title;
    self.rightLabel.text = rightTxt;
    
    if (b)
    {
        self.rightLabel.hidden = YES;
        self.btn1.hidden = NO;
        self.btn2.hidden = NO;
        self.btn3.hidden = NO;

        
        if([isPublic isEqualToString:@"0"]){
            if([seleStr isEqualToString:@"1"]){
                self.btn1.backgroundColor = bgmyelloeColor;
            }else if ([seleStr isEqualToString:@"2"]){
                self.btn2.backgroundColor = bgmyelloeColor;
            }else if ([seleStr isEqualToString:@""]){
                self.btn3.backgroundColor = bgmyelloeColor;
            }
            
            
        }else{
           
            if([seleStr isEqualToString:@"1"]){
                self.btn1.backgroundColor = bgmColor(211, 211, 211);
            }else if ([seleStr isEqualToString:@"2"]){
                self.btn2.backgroundColor = bgmColor(211, 211, 211);
            }else if ([seleStr isEqualToString:@""]){
                self.btn3.backgroundColor = bgmColor(211, 211, 211);
            }
        }
        
        
    }
    else
    {
        self.rightLabel.hidden = NO;
        self.btn1.hidden = YES;
        self.btn2.hidden = YES;
        self.btn3.hidden = YES;
    }
    
    if(swith){
       
        

    }else{
        if ([seleStr isEqualToString:@"1"]) {
            [self.rightSwitch setOn:YES];
        }else if([seleStr isEqualToString:@"2"]){
            [self.rightSwitch setOn:NO];
        }
        
        if([isPublic isEqualToString:@"0"]){
            self.rightSwitch.onTintColor = bgmyelloeColor;
            
            
        }else{
            self.rightSwitch.onTintColor = bgmColor(211, 211, 211);;
            
        }

    }
    
    self.rightSwitch.hidden = swith;
    
 
    
    if([isPublic isEqualToString:@"0"]){
        self.titleLabel.textColor = bgmmainColor;
        self.rightLabel.textColor = bgmgrayColor;
        [self.btn1 setTitleColor:bgmgrayColor forState:UIControlStateNormal];
        [self.btn2 setTitleColor:bgmgrayColor forState:UIControlStateNormal];
        [self.btn3 setTitleColor:bgmgrayColor forState:UIControlStateNormal];
        self.btn1.enabled = YES;
        self.btn2.enabled = YES;
        self.btn3.enabled = YES;
        self.rightSwitch.enabled = YES;

        
    }else{
        self.titleLabel.textColor = bgmsmallColor;
        self.rightLabel.textColor = bgmsmallColor;
        [self.btn1 setTitleColor:bgmsmallColor forState:UIControlStateNormal];
        [self.btn2 setTitleColor:bgmsmallColor forState:UIControlStateNormal];
        [self.btn3 setTitleColor:bgmsmallColor forState:UIControlStateNormal];
        self.btn1.enabled = NO;
        self.btn2.enabled = NO;
        self.btn3.enabled = NO;
        self.rightSwitch.enabled = NO;

        

    }
    
}
 
- (void)clickSexBtn:(UIButton *)btn{
    btn.selected = YES;
    btn.backgroundColor = bgmyelloeColor;
    [self.searHomeDelegate clickSex:[NSString stringWithFormat:@"%d",(int)btn.tag]];
    
    if (btn == self.btn1) {
        self.btn2.selected = NO;
        self.btn3.selected = NO;
        self.btn2.backgroundColor = [UIColor clearColor];
        self.btn3.backgroundColor = [UIColor clearColor];

    }else if(btn == self.btn2){
        self.btn1.selected = NO;
        self.btn3.selected = NO;
        self.btn1.backgroundColor = [UIColor clearColor];
        self.btn3.backgroundColor = [UIColor clearColor];

    }else if (btn == self.btn3){
        self.btn1.selected = NO;
        self.btn2.selected = NO;
        self.btn1.backgroundColor = [UIColor clearColor];
        self.btn2.backgroundColor = [UIColor clearColor];

    }
    
    
}
- (void)switchAction:(UISwitch *)switchButton{
    
    if (switchButton.isOn) {
        [self.searHomeDelegate clickPay:@"1"];
    }else{
        [self.searHomeDelegate clickPay:@"2"];
    }
    
}

@end
